package com.mentaleye.biz.impl;

import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mentaleye.bean.StudentInterviewAnalyzeBean;
import com.mentaleye.bean.UserLoginBean;
import com.mentaleye.biz.StudentInterviewAnalyzeBiz;
import com.mentaleye.dal.model.StudentInterviewAnalyze;
import com.mentaleye.dal.model.UserLogin;
import com.mentaleye.enums.DictionaryEnums;
import com.mentaleye.service.StudentInterviewAnalyzeService;
import com.mentaleye.service.UserLoginService;
import com.mentaleye.shiro.Constants;
import com.mentaleye.shiro.RedisCache;
import com.mentaleye.shiro.RedisKeyEnum;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;
import com.mentaleye.util.ObjectUtil;

@Service
public class StudentInterviewAnalyzeBizImpl implements StudentInterviewAnalyzeBiz {

	private static Logger logger = Logger.getLogger(StudentInterviewAnalyzeBiz.class);

	@Autowired
	private StudentInterviewAnalyzeService recordService;
	
	@Autowired 
	private UserLoginService loginService; 
	
	@Autowired private RedisCache<String, Object> redisCache;

	public CommonResult updateRecord(StudentInterviewAnalyzeBean recordBean) {
		CommonResult result = new CommonResult();
		try {
			Subject subject = SecurityUtils.getSubject();
			String loginName=(String) subject.getPrincipal();
			UserLoginBean loginBean = (UserLoginBean) this.redisCache.get(Constants.getRedisKey(RedisKeyEnum.USER_LOGIN, loginName));
			recordBean.setUserId(loginBean.getUserId());
			StudentInterviewAnalyze record = (StudentInterviewAnalyze) ObjectUtil.transfer(recordBean,
					StudentInterviewAnalyze.class);
			if(record.getRecordId()!=null){
				this.recordService.deleteByKey(record.getRecordId());
			}
			if (this.recordService.insertRecord(record)) {
				result.setIsSuccess(true);
				result.setResultMsg("修改成功");
			} else {
				result.setResultMsg("修改失败");
			}
		} catch (Exception e) {
			logger.error("修改面试情况分析异常", e);
			result.setResultMsg("修改面试情况分析异常");
		}
		return result;
	}
	
	public StudentInterviewAnalyzeBean getBeanByKey(long recordId) {
		StudentInterviewAnalyzeBean recordBean = null;
		try {
			recordBean = (StudentInterviewAnalyzeBean) ObjectUtil.transfer(
					this.recordService.getRecordByKey(recordId), StudentInterviewAnalyzeBean.class);
		} catch (Exception e) {
			logger.error("查询面试情况分析异常", e);
		}
		return recordBean;
	}

	public StudentInterviewAnalyzeBean getBeanByInterviewId(long interviewId) {
		StudentInterviewAnalyzeBean recordBean = null;
		try {
			recordBean = (StudentInterviewAnalyzeBean) ObjectUtil.transfer(
					this.recordService.getRecordByInterviewId(interviewId), StudentInterviewAnalyzeBean.class);
		} catch (Exception e) {
			logger.error("查询面试情况分析异常", e);
		}
		return recordBean;
	}

	public void getListByPage(CommonPage page) {
		Map<String, Object> pageMap = page.pageToMap();
		pageMap.put("userType", DictionaryEnums.TEACHER_TYPE.getCode());
		try {
			page.setPageData(this.recordService.getListByMap(pageMap));
		} catch (Exception e) {
			logger.error("查询面试情况分析异常", e);
		}
	}

	public CommonResult checkUser(long recordId) {
		CommonResult result = new CommonResult();
		Subject subject = SecurityUtils.getSubject();
		String userName = (String) subject.getPrincipal();
		try {
			UserLogin userLogin = this.loginService.getBySelective(userName, DictionaryEnums.TEACHER_TYPE.getCode());
			StudentInterviewAnalyze record = this.recordService.getRecordByKey(recordId);
			if(record.getUserId()==userLogin.getUserId()){
				result.setIsSuccess(true);
			}
			else
			{
				result.setResultMsg("没有权限修改别人的评价");
			}
		} catch (Exception e) {
			logger.error("查询面试情况分析异常", e);
			result.setResultMsg("查询面试情况分析异常");
		}
		return result;
	}
}
